package com.zoe.app.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.zoe.app.entity.Users;
import com.zoe.app.service.UsersService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zoe.Zhang
 * @date 2023/01/30
 * @description
 */
@RestController
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class UserController {
    private final UsersService usersService;

    @GetMapping("/add/{id}")
    public boolean add(@PathVariable String id) {
        Users users = new Users();
        // id 即使每次传的值是一样得,地址都会发生变化
        // id.intern() 则不会发生变化
        System.out.println(String.class.getName() + "@" + Integer.toHexString(System.identityHashCode(id.intern())));
        users.setUsername(IdUtil.fastSimpleUUID());
        users.setPassword("1111");
        System.out.println("===");
        System.out.println(String.class.getName() + "@" + Integer.toHexString(System.identityHashCode(users.getPassword())));
        users.setEnabled(false);
        return usersService.save(users);
    }

    @GetMapping("/addTran/")
    public boolean addTran() {
        return usersService.saveUser();
    }

    @GetMapping("/thisSave")
    public boolean thisSave() {
        return usersService.thisSave();
    }

    @GetMapping("/threadTran/{num}")
    public boolean threadTran(@PathVariable Integer num) {
        List<Users> list = CollUtil.newArrayList();
        for (int i = 0; i < 1000; i++) {
            Users users = new Users();
            users.setUsername(i + "");
            users.setPassword(RandomUtil.randomString(10));
            users.setEnabled(false);
            list.add(users);
        }
        return usersService.threadTransaction(list, num);
    }
}
